import g_get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import whoami from '../../atom/whoami';

import get from '@dfeidao/atom-web/local/get';
import error from '@dfeidao/atom-web/msgbox/error';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import p001 from './p001';
import p002 from './p002';
import p003 from './p003';
import p007 from './p007';

interface IReview {
	comment_numbering: string;
	comments: string;
	productid: string;
	commentator_numbering: string;
	dynamic_id: string;
	id: string;
	commentator_time: number;
	commentator_time_str: string;
	staff_name: string;
	headimg: string;
	statistical_magnitude: number;
	isa: number;
	auth_code: number;
	floor: number;
}
interface IActive {
	isa: number;
	dynamic_head: string;
}
function sub(param: string) {
	const index = param.indexOf('#');
	if (index > 0) {
		return param.substring(0, index);
	} else {
		return param;
	}
}
export default async function a001(fd: IFeidaoAiBrowserComponent) {
	// 评论查询
	const page_name = fd.data.node.getAttribute('page-name');
	let active_state = '300';
	set(fd, 'comment_numbering', '');
	const param = fd.data.params.dynamic_id as string;
	const dynamic_id = sub(param);
	const res = await whoami(fd);
	const a = res.params as { productid: string, staff_no: string };
	const staff_no = a.staff_no;
	const productid = a.productid;
	set(fd, 'productid', a.productid);
	set(fd, 'staff_no', staff_no);
	set(fd, 'dynamic_id', dynamic_id);
	let auth_code = '';
	try {
		show_loading();
		if (page_name === 'depart-details') {
			const dataArrays = await nodejs<{ dataArray: IActive[] }>(page_name + '/zj-000001/s001', { dynamic_id, staff_no });
			render(fd, dataArrays[0].isa, p001, 'p001', 'inner');
			set(fd, 'dynamic_head', dataArrays[0].dynamic_head);
			auth_code = g_get('depart_details_auth_code');
		} else if (page_name === 'sign-details') {
			auth_code = g_get('sign_details_auth_code');
		} else if (page_name === 'grab-details') {
			active_state = g_get('grab_details_active_state');
			auth_code = g_get('grab_details_auth_code');
		} else if (page_name === 'lottery-details') {
			auth_code = g_get('lottery_details_auth_code');
		}
		render(fd, active_state, p007, 'p007', 'inner');

		let page_no = get(fd, 'page_no') as number;
		if (!page_no) {
			page_no = 1;
		}
		const query = {
			dynamic_id,
			page_no,
			page_size: 5,
			employee_no: staff_no,
			auth_code,
			productid
		};
		set(fd, 'page_no', query.page_no);
		set(fd, 'page_size', query.page_size);
		const result = await nodejs<{ dataArray: IReview[], count: number, page_no: number, page_size: number; isgrab: number; }>(page_name + '/s002', query);
		hide_loading();
		result.page_no = query.page_no;
		result.page_size = query.page_size;
		if (result.count > 0) {
			fd.data.node.querySelector<HTMLDivElement>('[data-comment="no-comment"]').classList.add('display-n');
			if (result.count <= query.page_no * query.page_size) {
				fd.data.node.querySelector<HTMLDivElement>('[data-id="openmore"]').classList.add('display-n');
			}
			if (page_name === 'grab-details') {
				result.isgrab = 1;
			} else {
				result.isgrab = 0;
			}
			result.dataArray.forEach((aa, index) => {
				aa.floor = (index + 1) + (page_no - 1) * 5;
			});
		} else {
			fd.data.node.querySelector<HTMLDivElement>('[data-comment="comment"]').classList.add('display-n');
			fd.data.node.querySelector<HTMLDivElement>('[data-id="openmore"]').classList.add('display-n');
		}
		render(fd, result, p002, 'p002', 'inner');
		for (let i = 0; i < result.dataArray.length; i++) {
			render(fd, result.dataArray[i], p003, 'p003' + result.dataArray[i].comment_numbering, 'inner');
		}
	} catch (e) {
		error('操作失败');
		hide_loading();
	}
}
